package cn.iocoder.an.module.mold.controller.admin.equipmentkeepconfig;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import cn.iocoder.an.framework.excel.core.util.ExcelUtils;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.EquipmentManagePageReqVO;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.EquipmentManageRespVO;
import cn.iocoder.an.module.business.controller.admin.equipmenttype.vo.EquipmentTypePageReqVO;
import cn.iocoder.an.module.business.controller.admin.equipmenttype.vo.EquipmentTypeRespVO;
import cn.iocoder.an.module.business.dal.dataobject.equipmentmanage.EquipmentManageDO;
import cn.iocoder.an.module.business.dal.dataobject.equipmenttype.EquipmentTypeDO;
import cn.iocoder.an.module.business.service.equipmentmanage.EquipmentManageService;
import cn.iocoder.an.module.business.service.equipmenttype.EquipmentTypeService;
import cn.iocoder.an.module.mold.controller.admin.equipmentkeepconfig.vo.BindSaveOrDeleteVO;
import cn.iocoder.an.module.mold.controller.admin.equipmentkeepconfig.vo.EquipmentKeepConfigPageReqVO;
import cn.iocoder.an.module.mold.controller.admin.equipmentkeepconfig.vo.EquipmentKeepConfigRespVO;
import cn.iocoder.an.module.mold.controller.admin.equipmentkeepconfig.vo.EquipmentKeepConfigSaveReqVO;
import cn.iocoder.an.module.mold.dal.dataobject.equipmentkeepconfig.EquipmentKeepConfigDO;
import cn.iocoder.an.module.business.job.CreateEquipmentWaitJob;
import cn.iocoder.an.module.mold.job.CreateEquipmentDamageJob;
import cn.iocoder.an.module.mold.service.equipmentkeepconfig.EquipmentKeepConfigService;
import com.fhs.core.trans.anno.TransMethodResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 设备保养配置")
@RestController
@RequestMapping("/Damage/equipment-keep-config")
@Validated
@Controller("EquipmentKeepConfigControllerDamage")
public class EquipmentKeepConfigController {

    @Autowired
    private EquipmentKeepConfigService equipmentKeepConfigService;


    @Autowired
    private CreateEquipmentDamageJob createEquipmentDamageJob;

    @Autowired
    private EquipmentTypeService equipmentTypeService;


    @Autowired
    private EquipmentManageService equipmentManageService;

    @PostMapping("/batchConfig")
    @Operation(summary = "批量配置")
    public CommonResult<Integer>  createEquipmentKeepConfig(@RequestBody BindSaveOrDeleteVO vo) {
      return success(equipmentKeepConfigService.batchEquipmentKeepConfig(vo));
    }

    @PostMapping("/create")
    @Operation(summary = "创建设备保养配置")
    public CommonResult<Long> createEquipmentKeepConfig(@Valid @RequestBody EquipmentKeepConfigSaveReqVO createReqVO) {
        return success(equipmentKeepConfigService.createEquipmentKeepConfig(createReqVO));
    }

    @GetMapping("/handleJob")
    @Operation(summary = "创建设备保养配置")
    public void handleJob() throws Exception {
        createEquipmentDamageJob.execute("");
    }


    @PutMapping("/update")
    @Operation(summary = "更新设备保养配置")
    public CommonResult<Boolean> updateEquipmentKeepConfig(@Valid @RequestBody EquipmentKeepConfigSaveReqVO updateReqVO) {
        equipmentKeepConfigService.updateEquipmentKeepConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备保养配置")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteEquipmentKeepConfig(@RequestParam("id") Long id) {
        equipmentKeepConfigService.deleteEquipmentKeepConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备保养配置")
    public CommonResult<Boolean> deleteEquipmentKeepConfigList(@RequestParam("ids") List<Long> ids) {
        equipmentKeepConfigService.deleteEquipmentKeepConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备保养配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<EquipmentKeepConfigRespVO> getEquipmentKeepConfig(@RequestParam("id") Long id) {
        EquipmentKeepConfigDO equipmentKeepConfig = equipmentKeepConfigService.getEquipmentKeepConfig(id);
        return success(BeanUtils.toBean(equipmentKeepConfig, EquipmentKeepConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备保养配置分页")
    public CommonResult<PageResult<EquipmentKeepConfigRespVO>> getEquipmentKeepConfigPage(@Valid EquipmentKeepConfigPageReqVO pageReqVO) {
        PageResult<EquipmentKeepConfigDO> pageResult = equipmentKeepConfigService.getEquipmentKeepConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentKeepConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备保养配置 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentKeepConfigExcel(@Valid EquipmentKeepConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentKeepConfigDO> list = equipmentKeepConfigService.getEquipmentKeepConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备保养配置.xls", "数据", EquipmentKeepConfigRespVO.class,
                        BeanUtils.toBean(list, EquipmentKeepConfigRespVO.class));
    }


    @GetMapping("/damagePage")
    @Operation(summary = "获得设备分类分页")
    public CommonResult<PageResult<EquipmentTypeRespVO>> getEquipmentTypePage(@Valid EquipmentTypePageReqVO pageReqVO) {
        PageResult<EquipmentTypeDO> pageResult = equipmentTypeService.getEquipmentTypePage(pageReqVO);
        PageResult<EquipmentTypeRespVO> bean = BeanUtils.toBean(pageResult, EquipmentTypeRespVO.class);
        bean.getList().forEach(item -> {
            String allName = equipmentKeepConfigService.getAllName(1, item.getId());
            item.setKeepConfigName(allName);
        });
        return success(bean);
    }

    @GetMapping("/damagePageEquipment")
    @Operation(summary = "获得设备台账分页")
    @TransMethodResult
    public CommonResult<PageResult<EquipmentManageRespVO>> getEquipmentManagePage(@Valid EquipmentManagePageReqVO pageReqVO) {
        PageResult<EquipmentManageDO> pageResult = equipmentManageService.getEquipmentManagePage(pageReqVO);
        PageResult<EquipmentManageRespVO> bean = BeanUtils.toBean(pageResult, EquipmentManageRespVO.class);
        bean.getList().forEach(item -> {
            String allName = equipmentKeepConfigService.getAllName(2, item.getId());
            item.setKeepConfigName(allName);
        });
        return success(bean);
    }

}